//
//	Order.swift
//
//	Create by 可 张 on 12/8/2016
//	Copyright © 2016. All rights reserved.
//	Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import Foundation


class Order: NSObject {
    /// 地址
    var address : String!
    /// 城市
    var city : String!
    /// 创建时间
    var createtime : String!
    /// 距离
    var distance : Int!
    /// 区域
    var district : String?
    /// 高德云图id
    var id : Int!
    /// 图片
    var image : [AnyObject]?
    /// 经纬度字符串
    var location : String!
    /// 经纬度
    var locationPoint : AMapGeoPoint?
    /// 订单标题
    var name : String!
    /// 省
    var province : String!
    /// 更新时间
    var updatetime : String!
    /// 详细地址
    var addressDetailed : String!
    /// 预约时间
    var arrangeTime : Int!
    /// 联系人
    var contactName : String!
    /// 联系方式
    var contactPhoneNumber : String!
    /// 类型
    var type : Int!
    /// 用户id
    var userId : Int!
    /// 商家id
    var merchant_id : Int!
    /// 订单状态
    var status : Int!
    /// 订单id
    var orderID : Int!
    
    
    // MARK: - 公共方法
    class func loadOrders(center: String,finished:(models:[Order]?,error:String?)->()) {

        OrderDAO.getAroundOrder(center) { (result, error) in
            // 错误判断
            if result == nil {
                finished(models: nil, error: error)
                return
            }
            if error != nil {
                finished(models: nil, error: error)
                return
            }
            // 遍历数组,将字典转换为模型
            var models = [Order]()
            for dict in result! {
                models.append(Order(fromDictionary: dict))
            }
            // 回调闭包
            finished(models: models, error: nil)
        }
        
    }
    
    class func  loadOrderList(userID:Int,finished:(models:[Order]?,error:String?)->()) {
        OrderDAO.getOrderList(userID) { (result, error) in
            // 错误判断
            if result == nil {
                finished(models: nil, error: error)
                return
            }
            if error != nil {
                finished(models: nil, error: error)
                return
            }
            // 遍历数组,将字典转换为模型
            var models = [Order]()
            for dict in result! {
                models.append(Order(fromDictionary: dict))
            }
            // 回调闭包
            finished(models: models, error: nil)
        }
    }
    // MARK: - 内部方法
    /**
     * Instantiate the instance using the passed dictionary values to set the properties values
     */
    init(fromDictionary dictionary: NSDictionary){
        address = dictionary["_address"] as? String
        city = dictionary["_city"] as? String
        createtime = dictionary["_createtime"] as? String
        distance = dictionary["_distance"] as? Int
        district = dictionary["_district"] as? String
        id = dictionary["_id"] as? Int
        image = dictionary["_image"] as? [AnyObject]
        location = dictionary["_location"] as? String
        name = dictionary["_name"] as? String
        province = dictionary["_province"] as? String
        updatetime = dictionary["_updatetime"] as? String
        addressDetailed = dictionary["address_detailed"] as? String
        arrangeTime = dictionary["arrange_time"] as? Int
        contactName = dictionary["contact_name"] as? String
        contactPhoneNumber = dictionary["contact_phone_number"] as? String
        type = dictionary["type"] as? Int
        userId = dictionary["user_id"] as? Int
        merchant_id = dictionary["merchant_id"] as? Int
        status = dictionary["status"] as? Int
        orderID = dictionary["orderID"] as? Int
    }
    init(poi:AMapCloudPOI) {
        id = poi.uid
        name = poi.name
        locationPoint = poi.location
        address = poi.address
        createtime = poi.createTime
        updatetime = poi.updateTime
        distance = poi.distance
        city = poi.customFields["_city"] as? String
        contactName = poi.customFields["contact_name"] as? String
        contactPhoneNumber = poi.customFields["contact_phone_number"] as? String
        addressDetailed = poi.customFields["address_detailed"] as? String
        userId = poi.customFields["user_id"] as? Int
        type = poi.customFields["type"] as? Int
        arrangeTime = poi.customFields["arrange_time"] as? Int
        province = poi.customFields["_province"] as? String
        merchant_id = poi.customFields["merchant_id"] as? Int
        status = poi.customFields["status"] as? Int
        orderID = poi.customFields["orderID"] as? Int
    }
    
    /**
     * Returns all the available property values in the form of NSDictionary object where the key is the approperiate json key and the value is the value of the corresponding property
     */
    func toDictionary() -> NSDictionary
    {
        var dictionary = NSMutableDictionary()
        if address != nil{
            dictionary["_address"] = address
        }
        if city != nil{
            dictionary["_city"] = city
        }
        if createtime != nil{
            dictionary["_createtime"] = createtime
        }
        if distance != nil{
            dictionary["_distance"] = distance
        }
        if district != nil{
            dictionary["_district"] = district
        }
        if id != nil{
            dictionary["_id"] = id
        }
        if image != nil{
            dictionary["_image"] = image
        }
        if location != nil{
            dictionary["_location"] = location
        }
        if name != nil{
            dictionary["_name"] = name
        }
        if province != nil{
            dictionary["_province"] = province
        }
        if updatetime != nil{
            dictionary["_updatetime"] = updatetime
        }
        if addressDetailed != nil{
            dictionary["address_detailed"] = addressDetailed
        }
        if arrangeTime != nil{
            dictionary["arrange_time"] = arrangeTime
        }
        if contactName != nil{
            dictionary["contact_name"] = contactName
        }
        if contactPhoneNumber != nil{
            dictionary["contact_phone_number"] = contactPhoneNumber
        }
        if type != nil{
            dictionary["type"] = type
        }
        if userId != nil{
            dictionary["user_id"] = userId
        }
        return dictionary
    }
    
    
}